//
//  simMessageDetailViewController.m
//  simens
//
//  Created by xx xx on 12-7-19.
//  Copyright (c) 2012年 yst. All rights reserved.
//

#import "simMessageDetailViewController.h"
#import "simMessageViewController.h"
#import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h"

@interface simMessageDetailViewController ()
@property (nonatomic,strong) Message *message;
@property (nonatomic,strong) UIView *messageDetailView;
@property (nonatomic,strong) UITextField *smscontentField;
@property (nonatomic,strong) UIButton *agreeButton;
@property (nonatomic,strong) UIButton *refuseButton;
@property (nonatomic,strong) UITextView *textView;
@property (nonatomic,strong) UITextView *titleView;

@end


@implementation simMessageDetailViewController
@synthesize message = _message;
@synthesize messageDetailView = _messageDetailView;
@synthesize smscontentField = _smscontentField;
@synthesize agreeButton = _agreeButton;
@synthesize refuseButton = _refuseButton;
@synthesize textView=_textView;
@synthesize titleView = _titleView;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}




- (void)viewDidLoad

{   
    self.title=@"消息内容";

    [super viewDidLoad];
	// Do any additional setup after loading the view.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}



-(void) agreePress {
    
    [self showMbprogress];
    NSString *u =[NSString stringWithFormat:@"%@/recordSendWaitMess?moblie=%@&pwd=%@",[[NSUserDefaults standardUserDefaults]objectForKey:@"WebserviceUrl"],[[NSUserDefaults standardUserDefaults]objectForKey:@"LoginMobile"],[[NSUserDefaults standardUserDefaults]objectForKey:@"LoginPassword"]];
     
    NSMutableString *ms = [[NSMutableString alloc] init];
    [ms appendString:u];
    [ms appendString:@"&id="];
    [ms appendString:self.message.mid];
    [ms appendString:@"&result=TY"];
    [ms appendFormat:@"&status=Y"];
    
    NSURL *url = [NSURL URLWithString: ms];
    
    /*
    UIAlertView *alert5 = [[UIAlertView alloc] initWithTitle:@"消息" message:@"数据上传成功!"
                                                   delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [alert5 show];	
     */
    
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request startSynchronous];
    NSError *error = [request error];
    
    if (!error) {
        NSString *result = [request responseString];
        
        if([result isEqualToString:@"true"]){
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"消息" message:@"数据上传成功!"
                                                           delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
            [alert show];
            [self hideMbProgress];
           

            
        }else {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"消息" message:@"数据上传失败!"
                                                           delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
            [alert show];
            [self hideMbProgress];
        }
        
        
    }
    
    return;
    
}


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {    
    
    simMessageViewController* messageViewController = [[simMessageViewController alloc] init];
    
    [self.navigationController pushViewController:messageViewController animated:YES];
    
}  

-(void) refusePress {
    
    if([self.message.smstype isEqualToString:@"HY"]){
    [self showMbprogress];
    NSString *u =[NSString stringWithFormat:@"%@/recordSendWaitMess?moblie=%@&pwd=%@",[[NSUserDefaults standardUserDefaults]objectForKey:@"WebserviceUrl"],[[NSUserDefaults standardUserDefaults]objectForKey:@"LoginMobile"],[[NSUserDefaults standardUserDefaults]objectForKey:@"LoginPassword"]];
    
    NSMutableString *ms = [[NSMutableString alloc] init];
    [ms appendString:u];
    [ms appendString:@"&id="];
    [ms appendString:self.message.mid];
    [ms appendString:@"&result=JJ"];
    [ms appendFormat:@"&status=Y"];
    
    
    NSURL *url = [NSURL URLWithString: ms];
    
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request startSynchronous];
    NSError *error = [request error];
    
    
    if (!error) {
        NSString *result = [request responseString];
        
        if([result isEqualToString:@"true"]){
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"消息" message:@"数据上传成功!"
                                                           delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
            [alert show];
            [self hideMbProgress];
            simMessageViewController* messageViewController = [[simMessageViewController alloc] init];
            
            [self.navigationController pushViewController:messageViewController animated:YES];
            
        }else {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"消息" message:@"数据上传失败!"
                                                           delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
            [alert show];	
            [self hideMbProgress];
        }
        
        
    }
    }else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"消息" message:@"数据上传成功!"
                                                       delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];
        [self hideMbProgress];
        simMessageViewController* messageViewController = [[simMessageViewController alloc] init];
        
        [self.navigationController pushViewController:messageViewController animated:YES];
    }
    return;
    
}


-(void) setItems:(Message *) tt{
    self.message = tt;
    NSString *smscontent = self.message.smscontent;
    NSString *title = self.message.title;
    
    NSMutableString *temp = [[NSMutableString alloc] init];
    [temp appendString:@"标题：\n"];
    [temp appendString:title];
    [temp appendString:@"\n\n"];
    [temp appendString:@"消息内容：\n"];
    [temp appendString:smscontent];
    
    

    
    self.textView = [[UITextView alloc] initWithFrame:self.view.frame];
	self.textView.textColor = [UIColor blackColor];
	self.textView.font = [UIFont fontWithName:@"Arial" size:18.0];
	self.textView.delegate = self;
	self.textView.backgroundColor = [UIColor whiteColor];
	
	self.textView.text = temp ;
	self.textView.returnKeyType = UIReturnKeyDone;
	self.textView.keyboardType = UIKeyboardTypeDefault;	// use the default type input method (entire keyboard)
	self.textView.scrollEnabled = YES; 
    
	
	// this will cause automatic vertical resize when the table is resized
	self.textView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
	
	[self.view addSubview: self.textView];
    
    
    self.agreeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    
    if([self.message.smstype isEqualToString:@"HY"]){
        [self.agreeButton setFrame:CGRectMake(170,280,120,50)];
        [self.agreeButton setTitle:@"参加" forState:(UIControlStateNormal)];
    }
    if([self.message.smstype isEqualToString:@"TZ"]){
        [self.agreeButton setFrame:CGRectMake(70,280,170,50)];
        [self.agreeButton setTitle:@"已读" forState:(UIControlStateNormal)];
    }
    
    
    //定义点击时的响应函数
    if([self.message.state isEqualToString:@"N"]){
        [self.view addSubview: self.agreeButton];
	 [self.agreeButton addTarget:self action:@selector(agreePress) forControlEvents:UIControlEventTouchUpInside]; 
    }
    
    
    
    self.refuseButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [self.refuseButton setFrame:CGRectMake(25,280,120,50)];
    if([self.message.smstype isEqualToString:@"HY"]){
        [self.refuseButton setTitle:@"不参加" forState:(UIControlStateNormal)];
    }
    if([self.message.smstype isEqualToString:@"TZ"]){
        [self.refuseButton setTitle:@"未读" forState:(UIControlStateNormal)];
    }
    
    //定义点击时的响应函数
    if([self.message.state isEqualToString:@"N"]){
	[self.refuseButton addTarget:self action:@selector(refusePress) forControlEvents:UIControlEventTouchUpInside]; 
        if([self.message.smstype isEqualToString:@"HY"]){
            [self.view addSubview: self.refuseButton];
        }
    }
    
    
    
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"])
    {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}


@end
